package com.zk.demo.util;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

public class StringUtil {
    public static boolean isNotEmpty(String str) {
        return null != str && !"".equals(str);
    }

    public static boolean isEmpty(String str) {
        return null == str || "".equals(str);
    }

    /**
     * @param sourceStr  待替换字符串
     * @param matchStr   匹配字符串
     * @param replaceStr 目标替换字符串
     * @return
     */
    public static String replaceFirst(String sourceStr, String matchStr, String replaceStr) {
        int index = sourceStr.indexOf(matchStr);
        int matLength = matchStr.length();
        int sourLength = sourceStr.length();
        String beginStr = sourceStr.substring(0, index);
        String endStr = sourceStr.substring(index + matLength, sourLength);
        sourceStr = beginStr + replaceStr + endStr;
        return sourceStr;
    }


    /**
     * 正则提取器  by zk
     * @param Str
     * @param before
     * @param after
     * @return
     */
    public static String getWant(String Str,String before,String after){
        int begin = Str.indexOf(before)+before.length();
        int end= Str.replace(Str.substring(0,begin),"").indexOf(after)+begin;
        return Str.substring(begin,end);

    }


    /**
     * 判断字符串是否是json字符串
     * @param content
     * @return
     */
    public static boolean isJson(String content) {
        if(isEmpty(content)){
            return  false;
        }
        try {
            JSONObject.parse(content);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static  boolean isMatch(String s){
        //定义左右括号的对应关系
        Map<Character,Character> bracket  = new HashMap<>();
        bracket.put(')','(');
        bracket.put(']','[');
        bracket.put('}','{');

        Stack stack = new Stack();

        for(int i = 0; i < s.length(); i++){

            Character temp = s.charAt(i);//先转换成字符
            //是否为左括号
            if(bracket.containsValue(temp)){

                stack.push(temp);
                //是否为右括号
            }else if(bracket.containsKey(temp)){
                if(stack.isEmpty()){
                    return false;
                }
                //若左右括号匹配
                if(stack.peek() == bracket.get(temp)){
                    stack.pop();
                }
                else{
                    return false;
                }
            }

        }
        return stack.isEmpty()? true: false;

    }


    /**
     * 查询所有基金列表
     * @param args
     * @throws Exception
     */
    public static void main(String[] args)throws Exception {
        String s = HttpUtil.get("http://fund.eastmoney.com/js/fundcode_search.js", null, 5000, 5000);
        String want = getWant(s, "var r = ", ";");
        List <List<String>>list = JSONObject.parseObject(want, List.class);
        String sql ="insert into FundInfo (fund_code,short_name,name,type,full_name) values ";
        for (List<String> strings : list) {
            String  replace= strings.toString().replace("[", "(").replace("]", ")")+" ;\n";


            writeTxt(sql+replace,"d:/123.sql");
        }
        System.out.println("写入完成");


    }



    public static void writeTxt(String text, String writePath) {

        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(new File(writePath), true));
            writer.write(text);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
